package com.chalk.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.chalk.common.util.Result;
import com.chalk.exception.ColumnNotEmptyException;
import com.chalk.model.Picture;
import com.chalk.service.PictureService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author glq
 * @since 2020-12-19
 */

@RestController
@RequestMapping("/v1/picture")
public class PictureController {

    @Autowired
    PictureService pictureService;

    /**
     * 轮播图分页
     *
     * @param map
     * @return
     */
    @GetMapping("getPictureForPage")
    public Result getPictureForPage(@RequestParam Map<String, Object> map) throws ColumnNotEmptyException {
        Page<Picture> pictureForPage = pictureService.getPictureForPage(map);
        return Result.success(pictureForPage);
    }



    /**
     * 新增轮播图
     * @param picture
     * @return
     */
    @PostMapping("savePicture")
    public Result savePicture(@RequestBody Picture picture){

        System.out.println("========pictur为============"+picture.toString());

        if(StringUtils.isBlank(picture.getPictureUrl())){
            return Result.fail("轮播图url不能为空！");
        }
        Boolean f=pictureService.savePicture(picture);
        return f?Result.success("操作成功"):Result.fail("操作失败");
    }

    /**
     * 修改轮播图
     * @param picture
     * @return
     */
    @PutMapping("updatePicture")
    public Result updatePicture(@RequestBody Picture picture){
        System.out.println("picture对象为："+picture);
        if(picture.getPictureId()==null){
            return Result.fail("轮播图id不能为空！");
        }
        if(StringUtils.isBlank(picture.getPictureUrl())){
            return Result.fail("轮播图url不能为空！");
        }
        Boolean f=pictureService.updatePicture(picture);
        return f?Result.success("操作成功"):Result.fail("操作失败");
    }

    /**
     * @Author 86130
     * @Description //todo 删除轮播图
     * @methodName deletePicture
     * @param pictureId:
     * @return Result
     * @Date  2021/9/27 19:19
     **/
    @DeleteMapping("deletePicture")
    public Result deletePicture(Integer pictureId){
        if(pictureId==null){
            return Result.fail("轮播图id不能为空！");
        }
        Boolean f=pictureService.deletePicture(pictureId);
        System.out.println(f);
        return f?Result.success("操作成功"):Result.fail("操作失败");
    }

}

